document.addEventListener('DOMContentLoaded', function() {
    // 加载动画
    setTimeout(function() {
        document.querySelector('.loader-container').style.opacity = '0';
        setTimeout(function() {
            document.querySelector('.loader-container').style.display = 'none';
        }, 500);
    }, 2000);
    
    // 头部滚动效果
    const header = document.querySelector('.main-header');
    window.addEventListener('scroll', function() {
        if (window.scrollY > 50) {
            header.classList.add('scrolled');
        } else {
            header.classList.remove('scrolled');
        }
    });
    
    // 创建水墨动画效果
    createInkAnimation();
    
    // 导航菜单高亮
    const navLinks = document.querySelectorAll('.main-nav a');
    navLinks.forEach(link => {
        link.addEventListener('click', function() {
            navLinks.forEach(item => item.classList.remove('active'));
            this.classList.add('active');
        });
    });
    
    // 检查登录状态
    checkLoginStatus();
    
    // 导航链接点击事件
    document.getElementById('worksLink').addEventListener('click', function(e) {
        handlePageNavigation(e, 'works.html');
    });
    
    document.getElementById('philosophyLink').addEventListener('click', function(e) {
        handlePageNavigation(e, 'philosophy.html');
    });
    
    document.getElementById('techLink').addEventListener('click', function(e) {
        handlePageNavigation(e, 'digital-technology.html');
    });
    
    document.getElementById('aboutLink').addEventListener('click', function(e) {
        handlePageNavigation(e, 'about.html');
    });
    
    // 工作项目点击事件
    const workLinks = document.querySelectorAll('.work-link');
    workLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            handlePageNavigation(e, 'works.html');
        });
    });
    
    // 读取更多点击事件
    const readMoreLink = document.querySelector('.read-more');
    readMoreLink.addEventListener('click', function(e) {
        e.preventDefault();
        handlePageNavigation(e, 'philosophy.html');
    });
    
    // 处理页面导航
    function handlePageNavigation(e, page) {
        e.preventDefault();
        
        // 检查登录状态
        const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
        
        if (!isLoggedIn) {
            // 未登录，跳转到登录页面
            alert('您需要先登录才能查看其他页面');
            window.location.href = 'login.html?redirect=' + encodeURIComponent(page);
        } else {
            // 已登录，显示提示信息并跳转
            alert('您已登录，可以查看其他页面');
            window.location.href = page;
        }
    }
    
    // 检查登录状态
    function checkLoginStatus() {
        const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
        
        if (isLoggedIn) {
            // 已登录，可以在这里显示用户信息等
            console.log('用户已登录');
        } else {
            // 未登录
            console.log('用户未登录');
        }
    }
    
    // 为所有链接添加点击事件
    const allLinks = document.querySelectorAll('a:not([href^="#"])');
    allLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            const href = this.getAttribute('href');
            
            // 如果链接不是当前页面且不是站外链接
            if (!href.includes('index.html') && !href.includes('http') && !href.includes('#')) {
                e.preventDefault();
                
                // 检查登录状态
                const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
                
                if (!isLoggedIn) {
                    // 未登录，跳转到登录页面
                    alert('您需要先登录才能查看其他页面');
                    window.location.href = 'login.html?redirect=' + encodeURIComponent(href);
                } else {
                    // 已登录，显示提示信息并跳转
                    alert('您已登录，可以查看其他页面');
                    window.location.href = href;
                }
            }
        });
    });
});
// 为首页的作品详情链接添加点击事件
document.addEventListener('DOMContentLoaded', function() {
    // 首页作品详情链接
    const homeWorkLinks = document.querySelectorAll('.work-link');
    
    homeWorkLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            
            // 获取作品信息
            const workItem = this.closest('.work-item');
            const title = workItem.querySelector('h3').textContent;
            const category = workItem.querySelector('p').textContent.split('，')[0];
            const year = '2023';
            const technique = category.includes('交互') ? '交互式数字装置' : '数字绘画';
            const description = workItem.querySelector('p').textContent;
            const techniques = category.includes('交互') 
                ? ['传感器技术', '实时渲染', '交互编程'] 
                : ['数字绘画', '程序算法', '动态渲染'];
            const inspiration = `以中国传统${category.split('系列')[0]}为灵感，结合数字技术创作的${category.includes('交互') ? '交互式' : ''}作品。`;
            
            // 获取图片
            const imageUrl = workItem.querySelector('.work-image').getAttribute('data-src');
            
            // 打开作品详情模态框
            openWorkDetailModal(title, category, year, technique, description, techniques, inspiration, imageUrl);
        });
    });
    
    // 打开作品详情模态框
    function openWorkDetailModal(title, category, year, technique, description, techniques, inspiration, imageUrl) {
        // 填充模态框内容
        document.querySelector('.detail-title').textContent = title;
        document.querySelector('.detail-category').textContent = category;
        document.querySelector('.detail-year').textContent = `创作年份: ${year}`;
        document.querySelector('.detail-technique').textContent = `创作技术: ${technique}`;
        document.querySelector('.detail-description').textContent = description;
        
        // 填充创作技术列表
        const techniquesHTML = techniques.map(tech => 
            `<span class="technique-item">${tech}</span>`
        ).join('');
        document.querySelector('.techniques-list').innerHTML = techniquesHTML;
        
        // 填充创作灵感
        document.querySelector('.detail-inspiration p').textContent = inspiration;
        
        // 设置图片
        const detailImage = document.querySelector('.detail-image');
        detailImage.src = imageUrl;
        detailImage.alt = title;
        
        // 显示模态框
        const modal = document.getElementById('workDetailModal');
        modal.style.display = 'block';
        document.body.style.overflow = 'hidden'; // 防止背景滚动
    }
    
    // 关闭模态框
    document.querySelector('.close-modal').addEventListener('click', function() {
        document.getElementById('workDetailModal').style.display = 'none';
        document.body.style.overflow = 'auto';
    });
    
    window.addEventListener('click', function(event) {
        const modal = document.getElementById('workDetailModal');
        if (event.target === modal) {
            modal.style.display = 'none';
            document.body.style.overflow = 'auto';
        }
    });
});